package com.aidex.web.controller.biz;

import com.aidex.biz.domain.AccountInfo;
import com.aidex.biz.service.AccountInfoService;
import com.aidex.common.annotation.Log;
import com.aidex.common.core.controller.BaseController;
import com.aidex.common.core.domain.R;
import com.aidex.common.core.page.PageDomain;
import com.aidex.common.enums.BusinessType;
import com.aidex.common.utils.SecurityUtils;
import com.aidex.common.utils.poi.ExcelUtil;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 会员账户信息Controller
 *
 * @author sean
 * @email 2074844820@qq.com
 * @date 2022-10-26
 */
@RestController
@RequestMapping("/biz/bizAccountInfo")
public class AccountInfoController extends BaseController {
    @Autowired
    private AccountInfoService accountInfoService;

    /**
     * 查询会员账户信息列表
     */
    @PreAuthorize("@ss.hasPermi('biz:bizAccountInfo:list')")
    @GetMapping("/list")
    public R<PageInfo> list(AccountInfo accountInfo, HttpServletRequest request, HttpServletResponse response) {
        if (!SecurityUtils.isAdmin(SecurityUtils.getUserId())) {
            accountInfo.setCompanyId(SecurityUtils.getLoginUser().getUser().getCompanyId());
        }
        accountInfo.setPage(new PageDomain(request, response));
        return R.data(accountInfoService.findPage(accountInfo));
    }

    /**
     * 获取会员账户信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('biz:bizAccountInfo:query')")
    @GetMapping(value = "/{id}")
    public R<AccountInfo> detail(@PathVariable("id") String id) {
        return R.data(accountInfoService.get(id));
    }

    /**
     * 新增会员账户信息
     */
    @PreAuthorize("@ss.hasPermi('biz:bizAccountInfo:add')")
    @Log(title = "会员账户信息", businessType = BusinessType.INSERT)
    @PostMapping
    public R add(@RequestBody @Validated AccountInfo accountInfo) {
        return R.status(accountInfoService.save(accountInfo));
    }

    /**
     * 修改会员账户信息
     */
    @PreAuthorize("@ss.hasPermi('biz:bizAccountInfo:edit')")
    @Log(title = "会员账户信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public R edit(@RequestBody @Validated AccountInfo accountInfo) {
        return R.status(accountInfoService.save(accountInfo));
    }


    /**
     * 删除会员账户信息
     */
    @PreAuthorize("@ss.hasPermi('biz:bizAccountInfo:remove')")
    @Log(title = "会员账户信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R remove(@PathVariable String[] ids) {
        return R.status(accountInfoService.deleteAccountInfoByIds(ids));
    }

    /**
     * 获取最大编号
     */
    @PreAuthorize("@ss.hasPermi('biz:bizAccountInfo:query')")
    @GetMapping("/findMaxSort")
    public R findMaxSort() {
        return R.data(accountInfoService.findMaxSort(new AccountInfo()));
    }

    /**
     * 导出会员账户信息列表
     */
    @PreAuthorize("@ss.hasPermi('biz:bizAccountInfo:export')")
    @Log(title = "会员账户信息", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public R export(AccountInfo accountInfo) {

        if (SecurityUtils.isAdmin(SecurityUtils.getUserId())) {
            accountInfo.setCompanyId(null);
        } else {
            accountInfo.setCompanyId(SecurityUtils.getLoginUser().getUser().getCompanyId());
        }
        List<AccountInfo> list = accountInfoService.findList(accountInfo);
        ExcelUtil<AccountInfo> util = new ExcelUtil<AccountInfo>(AccountInfo.class);
        return util.exportExcel(list, "会员账户信息数据");
    }

}
